<template>
  <div class="playlist-container">
    <div class="playlist-header">
      <h2>我的歌单</h2>
      <el-button type="primary" :icon="Plus" @click="showCreateDialog = true">
        新建歌单
      </el-button>
    </div>
    
    <div class="playlists-grid">
      <div v-for="playlist in playlists" :key="playlist.id" class="playlist-card">
        <img :src="playlist.cover" :alt="playlist.name" class="playlist-cover" />
        <div class="playlist-info">
          <div class="playlist-name">{{ playlist.name }}</div>
          <div class="playlist-count">{{ playlist.count }}首歌曲</div>
          <div class="playlist-actions">
            <el-button size="small" text @click="editPlaylist(playlist)">编辑</el-button>
            <el-button size="small" text type="danger" @click="deletePlaylist(playlist)">删除</el-button>
          </div>
        </div>
      </div>
    </div>
    
    <el-dialog v-model="showCreateDialog" title="新建歌单" width="400px">
      <el-form :model="newPlaylist">
        <el-form-item label="歌单名称">
          <el-input v-model="newPlaylist.name" placeholder="请输入歌单名称" />
        </el-form-item>
        <el-form-item label="歌单描述">
          <el-input 
            v-model="newPlaylist.description" 
            type="textarea" 
            placeholder="请输入歌单描述"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showCreateDialog = false">取消</el-button>
        <el-button type="primary" @click="createPlaylist">创建</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'

const playlists = ref([
  { id: 1, name: '我喜欢的音乐', cover: 'https://via.placeholder.com/150', count: 25 },
  { id: 2, name: '流行音乐', cover: 'https://via.placeholder.com/150', count: 50 },
  { id: 3, name: '轻音乐', cover: 'https://via.placeholder.com/150', count: 30 }
])

const showCreateDialog = ref(false)
const newPlaylist = ref({
  name: '',
  description: ''
})

const createPlaylist = () => {
  if (newPlaylist.value.name.trim()) {
    playlists.value.push({
      id: Date.now(),
      name: newPlaylist.value.name,
      cover: 'https://via.placeholder.com/150',
      count: 0
    })
    newPlaylist.value = { name: '', description: '' }
    showCreateDialog.value = false
  }
}

const editPlaylist = (playlist: any) => {
  console.log('编辑歌单:', playlist)
}

const deletePlaylist = (playlist: any) => {
  const index = playlists.value.findIndex(p => p.id === playlist.id)
  if (index > -1) {
    playlists.value.splice(index, 1)
  }
}
</script>

<style scoped>
.playlist-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.playlist-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.playlist-header h2 {
  margin: 0;
  color: #303133;
}

.playlists-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
}

.playlist-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: transform 0.2s;
}

.playlist-card:hover {
  transform: translateY(-2px);
}

.playlist-cover {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
}

.playlist-info {
  padding: 15px;
}

.playlist-name {
  font-size: 16px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 5px;
}

.playlist-count {
  font-size: 14px;
  color: #909399;
  margin-bottom: 10px;
}

.playlist-actions {
  display: flex;
  gap: 10px;
}
</style>
